package cn.jsc.quartz.job;

import cn.jsc.order.domain.AdoptOrder;
import cn.jsc.order.domain.ProductOrder;
import cn.jsc.order.mapper.AdoptOrderMapper;
import cn.jsc.order.mapper.ProductOrderMapper;
import cn.jsc.pay.domain.PayBill;
import cn.jsc.pay.mapper.PayBillMapper;
import cn.jsc.product.domain.Product;
import cn.jsc.product.mapper.ProductMapper;
import cn.jsc.quartz.constants.JobConstants;
import cn.jsc.quartz.dto.QuartzJobInfo;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

public class MainJob implements Job {
    @Autowired
    private AdoptOrderMapper adoptOrderMapper;
    @Autowired
    private PayBillMapper payBillMapper;
    @Autowired
    private ProductOrderMapper productOrderMapper;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //根据不同的类型 type 区分不同的业务
        // jobDataMap.put("params", params);
        // 获取传递的参数：info 就可以强转成info  //添加定时任务最底层就是调用工具类，工具类中添加参数put("params", params);
        QuartzJobInfo info = (QuartzJobInfo) jobExecutionContext.getJobDetail().getJobDataMap().get("params");
        if(info.getType().equals(JobConstants.OVER_TIME_NO_PAY_ADOPT)){//领养订单超时未支付
            //获取参数 - 修改订单或支付单的状态  - 找到支付单或订单【id，paySn】
            Map<String, Object> map = info.getParams();
            long orderId = Long.parseLong(map.get("orderId").toString());
            //修改订单状态
            AdoptOrder adoptOrder = adoptOrderMapper.loadById(orderId);
            adoptOrder.setState(-1);//取消订单
            adoptOrderMapper.update(adoptOrder);//更新

            //修改支付单状态
            PayBill payBill = payBillMapper.loadByUnionPaySn(adoptOrder.getPaySn());
            payBill.setState(-1);//取消支付
            payBillMapper.update(payBill);//更新
        }else if(info.getType().equals(JobConstants.OVER_TIME_NO_PAY_PRODUCT)){//服务订单超时未支付
            //获取参数 - 修改订单或支付单的状态  - 找到支付单或订单【id，paySn】
            Map<String, Object> map = info.getParams();
            long orderId = Long.parseLong(map.get("orderId").toString());
            //修改订单状态
            ProductOrder productOrder = productOrderMapper.loadById(orderId);
            productOrder.setState(-1);//取消订单
            productOrderMapper.update(productOrder);

            //修改支付单状态
            PayBill payBill = payBillMapper.loadByUnionPaySn(productOrder.getPaySn());
            payBill.setState(-1);//取消支付
            payBillMapper.update(payBill);//更新
        }
    }
}